import math

earthCircumferenceMeters = 1000 * 40075.017

def earthMetersToRadians(meters):
    return (2 * math.pi) * (float(meters) / earthCircumferenceMeters)
    
def computerOffsetPosition(lon, lat, brng, dist):
    lat1 = lat * math.pi / 180
    lon1 = lon * math.pi / 180
    brg = brng * math.pi / 180

    # 扁率
    flat = 298.257223563
    # 地球 半长轴
    a = 6378137.0
    # 地球 半短轴
    b = 6356752.314245

    f = 1 / flat
    sb = math.sin(brg)
    cb = math.cos(brg)
    tu1 = (1 - f) * math.tan(lat1)
    cu1 = 1 / math.sqrt((1 + tu1 * tu1))
    su1 = tu1 * cu1
    s2 = math.atan2(tu1, cb)
    sa = cu1 * sb
    csa = 1 - sa * sa
    us = csa * (a * a - b * b) / (b * b)
    A = 1 + us / 16384 * (4096 + us * (-768 + us * (320 - 175 * us)))
    B = us / 1024 * (256 + us * (-128 + us * (74 - 47 * us)))
    s1 = dist / (b * A)
    s1p = 2 * math.pi
    cs1m = 0.0
    ss1 = 0.0
    cs1 = 0.0
    ds1 = 0.0
    #
    while abs(s1 - s1p) > 1e-12:
        cs1m = math.cos(2 * s2 + s1)
        ss1 = math.sin(s1)
        cs1 = math.cos(s1)
        ds1 = B * ss1 * (cs1m + B / 4 * (cs1 * (-1 + 2 * cs1m * cs1m) - B / 6 * cs1m * (-3 + 4 * ss1 * ss1) * (-3 + 4 * cs1m * cs1m)))
        s1p = s1
        s1 = dist / (b * A) + ds1

    t = su1 * ss1 - cu1 * cs1 * cb
    lat2 = math.atan2(su1 * cs1 + cu1 * ss1 * cb, (1 - f) * math.sqrt(sa * sa + t * t))
    l2 = math.atan2(ss1 * sb, cu1 * cs1 - su1 * ss1 * cb)
    c = f / 16 * csa * (4 + f * (4 - 3 * csa))
    l = l2 - (1 - c) * f * sa * (s1 + c * ss1 * (cs1m + c * cs1 * (-1 + 2 * cs1m * cs1m)))
    lon2 = lon1 + l
    Longitude = lon2 * 180 / math.pi
    Latitude = lat2 * 180 / math.pi
    return [Longitude, Latitude]